package org.jeecgframework.web.system.controller.core;

import com.tchy.cloud.tools.ImageUtils;
import com.tchy.cloud.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/03.
 * 文件上传控制层
 */
@Controller
@RequestMapping("/myFileUpload")
public class MyFileUploadController {


    /**
     *
     *@描述:附件文件Ajax上传
     *@return ResponseEntity<?>
     *@作者：zjy
     *@创建日期： 2014年5月26日09:48:59
     */
    @RequestMapping(value = "/upload_resource_file", method = RequestMethod.POST)
    public  void upload_resource_file(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,final HttpServletResponse response, ModelMap model) {
        try {


            //读取上传文件相对路径
//            String fileUrl = ReadProperties.getInstance().getValue("/META-INF/system.properties","mis_flie_upload_url");
            String fileUrl = "upload/files";
            //得到上传文件绝对路径
            String path = request.getSession().getServletContext().getRealPath(fileUrl);
           // String fileName = file.getOriginalFilename(); //文件原名

            /** 获取文件的后缀* */
            String suffix = file.getOriginalFilename().substring(
                    file.getOriginalFilename().lastIndexOf("."));

            //文件名以时间戳命名
            String fileName = System.currentTimeMillis() + suffix;

            //去掉文件名中的空格，避免下载出错
            fileName = fileName.replaceAll(" ","");

            if("".equals(fileName)){
                return;
            }
            //文件夹名
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String folderName = sdf.format(new Date().getTime());


            path += "//"+folderName;

            //创建文件夹及文件
            File targetFile = Tools.getFileByOriginalFileName(path, fileName, 2);


            //保存文件
            file.transferTo(targetFile);

            String new_fileName = targetFile.getName();



            String strUrl = fileUrl+"/" + folderName +"/"+ new_fileName;

            //设置返回头
            response.setContentType("text/html");
            response.getWriter().print(strUrl+","+new_fileName);

        } catch (Exception e) {
            //log.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }


    /**
     *
     *@描述:图片上传 并且自动生成两种尺寸图片
     *@return ResponseEntity<?>
     *@作者：zjy
     *@创建日期： 2015年3月31日 15:53:59
     */
    @RequestMapping(value = "/uploadImageScale", method = RequestMethod.POST)
    public  void uploadImageScale(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,final HttpServletResponse response, ModelMap model) {
        try {


            //读取上传文件相对路径
//            String fileUrl = ReadProperties.getInstance().getValue("/META-INF/system.properties","mis_flie_upload_url");
            String fileUrl = "upload/goodsImg";
            //得到上传文件绝对路径
            String path = request.getSession().getServletContext().getRealPath(fileUrl);
           // String fileName = file.getOriginalFilename(); //文件原名

            /** 获取文件的后缀* */
            String suffix = file.getOriginalFilename().substring(
                    file.getOriginalFilename().lastIndexOf("."));

            //文件名以时间戳命名
            Long ctm = System.currentTimeMillis();
            String fileName = ctm + suffix;//原文件
            String fileName_big = ctm + "_big" + suffix;//大图名
            String fileName_zoom = ctm + "_zoom" + suffix;//缩放名

            //去掉文件名中的空格，避免下载出错
            fileName = fileName.replaceAll(" ","");
            fileName_big = fileName_big.replaceAll(" ","");
            fileName_zoom = fileName_zoom.replaceAll(" ","");

            if("".equals(fileName)){
                return;
            }
            //文件夹名
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String folderName = sdf.format(new Date().getTime());


            path += "/"+folderName;

            //创建文件夹及文件
            File targetFile = Tools.getFileByOriginalFileName(path, fileName, 2);


            //保存原文件
            file.transferTo(targetFile);

            //生成大图
            ImageUtils.scale2(targetFile.getPath(), path + "/" + fileName_big, 800, 800, true);
            //生成缩略图
            ImageUtils.scale2(targetFile.getPath(), path + "/" + fileName_zoom, 400, 400, true);

            String new_fileName = targetFile.getName();



            String strUrl = fileUrl+"/" + folderName +"/"+ new_fileName;
            String bigImgUrl = fileUrl+"/" + folderName +"/"+ fileName_big;
            String zoomImgUrl = fileUrl+"/" + folderName +"/"+ fileName_zoom;

            //设置返回头
            response.setContentType("text/html");
            response.getWriter().print(strUrl+","+new_fileName+","+bigImgUrl+","+zoomImgUrl);

        } catch (Exception e) {
            //log.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }







}
